//
//  YXNewFeatureViewController.swift
//  yanXiangWeiBo
//
//  Created by 颜祥 on 15/12/17.
//  Copyright © 2015年 yanxiang. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class YXNewFeatureViewController: UICollectionViewController
{
    // MARK: - 定义全局属性，cell的个数
    private let itemCount = 4
    
    // 流水布局
    let layout = UICollectionViewFlowLayout()
    
    init() {
        
        super.init(collectionViewLayout: layout)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    override func viewDidLoad()
    {
        super.viewDidLoad()

        // 注册cell
        self.collectionView!.registerClass(YXNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        // 设置item的属性
        // 尺寸和屏幕一样大
        layout.itemSize = UIScreen.mainScreen().bounds.size
        
        // 不要间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        // 设置水平滚动
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        // 设置分页显示
        collectionView?.pagingEnabled = true
        
        // 去掉弹簧
        collectionView?.bounces = false
        
    }

    
    

    // MARK: UICollectionViewDataSource

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        
        return itemCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! YXNewFeatureCell
        
        // 获得当前图片的索引
        cell.index = indexPath.item
    
        return cell
    }
    
    // 界面显示后
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        // 获得正在显示的cell的indexPath
        let VisibleIndexPath = collectionView.indexPathsForVisibleItems().last!
        
        // 如果此时是最后一张图片
        if VisibleIndexPath.item == itemCount - 1
        {
            // 获得对应的cell
            let cell = collectionView.cellForItemAtIndexPath(VisibleIndexPath) as! YXNewFeatureCell
            
            // 开始按钮动画
            cell.startButtonAnimation()
        }
    }
    
    

}


// MARK: - 自定义cell
class YXNewFeatureCell: UICollectionViewCell
{
    // 根据不同index显示不同的图片
    var index = 0{
    
        didSet{
            
            // 根据index设置对应的背景图片
            iconView.image = UIImage(named: "new_feature_\(index + 1)")
            
            // 每次都隐藏按钮
            startButton.hidden = true
        }
    
    }
    
    // 按钮的动画
    private func startButtonAnimation()
    {
        // 显示按钮
        startButton.hidden = false
        
        // 设置Scale
        startButton.transform = CGAffineTransformMakeScale(0, 0)
    
        // 开始动画
        UIView.animateWithDuration(0.8, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            // 清空transform
            self.startButton.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                
                print("按钮动画完成")
        }
        
        
    }
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 显示视图
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 显示视图
    private func prepareUI()
    {
        // 添加子控件
        contentView.addSubview(iconView)
        contentView.addSubview(startButton)
        
        // 添加约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
        
        // 图片
       
        contentView.addConstraint( NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        
        
        // 按钮
        
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        
        
    }
    
    // MARK: - 懒加载
    // 图片
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    // 按钮
    private lazy var startButton: UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
        
        // 设置背景图片
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        // 设置文字
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        
        // 添加点击事件
        btn.addTarget(self, action: "jumpToMainController", forControlEvents: UIControlEvents.TouchUpInside)
        
        
        return btn
    }()
    
    func jumpToMainController()
    {
        print("开始体验")
        // 跳转到YXWelcomeViewController
        // 调用AppDelegate来切换根控制器,不能直接创建AppDelegate(),需要使用系统维护的AppDelegate
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        appDelegate.switchRootViewController(true)
        
        
    }
   
    
    
}
